#include "StaticBNF.h"

void		StaticBNF::createScannerMap(void)
{
  this->library.push_back(this->buildHAI());
  this->library.push_back(this->buildCANHAS());
  this->library.push_back(this->buildVISIBLE());
  this->library.push_back(this->buildBTW());
  this->library.push_back(this->buildKTHXBYE());
  this->library.push_back(this->buildGTFO());
  this->library.push_back(this->buildKTHX());
  this->library.push_back(this->buildIZ());
  this->library.push_back(this->buildNOWAI());
  this->library.push_back(this->buildIHASA());
  this->library.push_back(this->buildDIAF());
  this->library.push_back(this->buildLOL());
  this->library.push_back(this->buildIMINYR());
  this->library.push_back(this->buildYARLY());
  this->library.push_back(this->buildBYES());
  this->library.push_back(this->buildUPZ());
  this->library.push_back(this->buildNERFZ());
  this->library.push_back(this->buildTIMEZD());
  this->library.push_back(this->buildOVARZ());
  this->library.push_back(this->buildGIMMEH());
}

StaticBNF::lex *		StaticBNF::buildHAI(void)
{
  lex		*l0 = new lex;
  l0->key = BEGIN;
  l0->exp = NONE;
  l0->lol = "HAI";
  l0->cpp = "\nint\tmain()\n{";
  l0->lol_end = "";
  l0->cpp_end = "";
  l0->do_convert = false;
  l0->priority = P_RIGHT;
  return l0;
}


StaticBNF::lex *		StaticBNF::buildCANHAS(void)
{
  lex		*l1 = new lex;
  l1->key = INCLUDE;
  l1->exp = VINCLUDE;
  l1->lol = "CAN HAS";
  l1->cpp = "#include ";
  l1->lol_end = "?";
  l1->cpp_end = "";
  l1->do_convert = true;
  l1->convert_args.insert(std::pair<std::string, std::string > ("STDIO", "<iostream>"));
  l1->convert_args.insert(std::pair<std::string, std::string>("STDLIB", "<cstdlib>"));
  l1->priority = P_RIGHT;
  return l1;
}


StaticBNF::lex *		StaticBNF::buildVISIBLE(void)
{
  lex		*l2 = new lex;
  l2->key = ECHO;
  l2->exp = VAR;
  l2->lol = "VISIBLE";
  l2->cpp = "std::cout <<";
  l2->lol_end = "";
  l2->cpp_end = ";";
  l2->do_convert = false;
  l2->priority = P_RIGHT;
  return l2;
}

StaticBNF::lex *		StaticBNF::buildBTW(void)
{
  lex		*l3 = new lex;
  l3->key = COMMENT;
  l3->exp = ELSE;
  l3->lol = "BTW";
  l3->cpp = "//";
  l3->lol_end = "";
  l3->cpp_end = "";
  l3->do_convert = false;
  l3->priority = P_RIGHT;
  return l3;
}

StaticBNF::lex *		StaticBNF::buildKTHXBYE(void)
{
  lex		*l4 = new lex;
  l4->key = END;
  l4->exp = NONE;
  l4->lol = "KTHXBYE";
  l4->cpp = "return 0;\n}";
  l4->lol_end = "";
  l4->cpp_end = "";
  l4->do_convert = false;
  l4->priority = P_RIGHT;
  return l4;
}

StaticBNF::lex *		StaticBNF::buildGTFO(void)
{
  lex		*l5 = new lex;
  l5->key = END;
  l5->exp = NONE;
  l5->lol = "GTFO";
  l5->cpp = "break;";
  l5->lol_end = "";
  l5->cpp_end = "";
  l5->do_convert = false;
  l5->priority = P_RIGHT;
  return l5;
}

StaticBNF::lex *		StaticBNF::buildKTHX(void)
{
  lex		*l6 = new lex;
  l6->key = END;
  l6->exp = NONE;
  l6->lol = "KTHX";
  l6->cpp = "}";
  l6->lol_end = "";
  l6->cpp_end = "";
  l6->do_convert = false;
  l6->priority = P_RIGHT;
  return l6;
}

StaticBNF::lex *		StaticBNF::buildIZ(void)
{
  lex		*l7 = new lex;
  l7->key = COND;
  l7->exp = CODE;
  l7->lol = "IZ ";
  l7->cpp = "if (";
  l7->lol_end = "?";
  l7->cpp_end = ")";
  l7->do_convert = true;
  l7->convert_args.insert(std::pair<std::string, std::string > (" BIGR THAN ",  " > "));
  l7->convert_args.insert(std::pair<std::string, std::string > (" SMALR THAN ",  " < "));
  l7->convert_args.insert(std::pair<std::string, std::string > (" LIEK ",  " == "));
  l7->priority = P_LEFT;
  return l7;
}

StaticBNF::lex *		StaticBNF::buildNOWAI(void)
{
  lex		*l8 = new lex;
  l8->key = COND;
  l8->exp = NONE;
  l8->lol = "NOWAI";
  l8->cpp = "} else {";
  l8->lol_end = "";
  l8->cpp_end = "";
  l8->do_convert = false;
  l8->priority = P_LEFT;
  return l8;
}

StaticBNF::lex *		StaticBNF::buildIHASA(void)
{
  lex		*l9 = new lex;
  l9->key = DECL;
  l9->exp = VAR;
  l9->lol = "I HAS A ";
  l9->cpp = "";
  l9->lol_end = "";
  l9->cpp_end = ";\n";
  l9->do_convert = true;

  for (std::map<std::string, std::string>::iterator it = this->varmap.begin();
       it != this->varmap.end(); ++it)
    {
      std::string tmp;
      tmp += (*it).second;
      tmp += (*it).first;
      l9->convert_args.insert(std::pair<std::string, std::string > ((*it).first, tmp));
    }

  l9->convert_args.insert(std::pair<std::string, std::string > (" ITZ ",  " = "));
  l9->priority = P_RIGHT;
  return l9;
}

StaticBNF::lex *		StaticBNF::buildDIAF(void)
{
  lex		*l10 = new lex;
  l10->key = END;
  l10->exp = CODE;
  l10->lol = "DIAF";
  l10->cpp = "return ";
  l10->lol_end = "";
  l10->cpp_end = ";";
  l10->do_convert = false;
  l10->priority = P_RIGHT;
  return l10;
}

StaticBNF::lex *		StaticBNF::buildLOL(void)
{
  lex		*l11 = new lex;
  l11->key = DECL;
  l11->exp = VAR;
  l11->lol = "LOL ";
  l11->cpp = "";
  l11->lol_end = "";
  l11->cpp_end = ";";
  l11->do_convert = true;
  l11->convert_args.insert(std::pair<std::string, std::string > (" R ",  " = "));
  l11->priority = P_RIGHT;
  return l11;
}

StaticBNF::lex *		StaticBNF::buildIMINYR(void)
{
  lex		*l12 = new lex;
  l12->key = COND;
  l12->exp = VAR;
  l12->lol = "IM IN YR ";
  l12->cpp = "while (";
  l12->lol_end = "";
  l12->cpp_end = ") {";
  l12->do_convert = true;
  l12->convert_args.insert(std::pair<std::string, std::string > (" R ",  " = "));
  l12->priority = P_RIGHT;
  return l12;
}

StaticBNF::lex *		StaticBNF::buildYARLY(void)
{
  lex		*l13 = new lex;
  l13->key = DECL;
  l13->exp = NONE;
  l13->lol = "YARLY";
  l13->cpp = "{";
  l13->lol_end = "";
  l13->cpp_end = "";
  l13->do_convert = false;
  l13->priority = P_RIGHT;
  return l13;
}

StaticBNF::lex *		StaticBNF::buildBYES(void)
{
  lex		*l14 = new lex;
  l14->key = END;
  l14->exp = CODE;
  l14->lol = "BYES ";
  l14->cpp = "exit(";
  l14->lol_end = "";
  l14->cpp_end = ");";
  l14->do_convert = false;
  l14->priority = P_RIGHT;
  return l14;
}

StaticBNF::lex *		StaticBNF::buildUPZ(void)
{
  lex		*l15 = new lex;
  l15->key = OPERANDE;
  l15->exp = VAR;
  l15->lol = "UPZ ";
  l15->cpp = "";
  l15->lol_end = "";
  l15->cpp_end = ";";
  l15->do_convert = true;
  l15->convert_args.insert(std::pair<std::string, std::string > ("!!",  " += "));
  l15->priority = P_RIGHT;
  return l15;
}

StaticBNF::lex *		StaticBNF::buildNERFZ(void)
{
  lex		*l16 = new lex;
  l16->key = OPERANDE;
  l16->exp = VAR;
  l16->lol = "NERFZ ";
  l16->cpp = "";
  l16->lol_end = "";
  l16->cpp_end = ";";
  l16->do_convert = true;
  l16->convert_args.insert(std::pair<std::string, std::string > ("!!",  " -= "));
  l16->priority = P_RIGHT;
  return l16;
}

StaticBNF::lex *		StaticBNF::buildTIMEZD(void)
{
  lex		*l17 = new lex;
  l17->key = OPERANDE;
  l17->exp = VAR;
  l17->lol = "TIMEZD ";
  l17->cpp = "";
  l17->lol_end = "";
  l17->cpp_end = ";";
  l17->do_convert = true;
  l17->convert_args.insert(std::pair<std::string, std::string > ("!!",  " *= "));
  l17->priority = P_RIGHT;
  return l17;
}

StaticBNF::lex *		StaticBNF::buildOVARZ(void)
{
  lex		*l18 = new lex;
  l18->key = OPERANDE;
  l18->exp = VAR;
  l18->lol = "OVARZ ";
  l18->cpp = "";
  l18->lol_end = "";
  l18->cpp_end = ";";
  l18->do_convert = true;
  l18->convert_args.insert(std::pair<std::string, std::string > ("!!",  " /= "));
  l18->priority = P_RIGHT;
  return l18;
}

StaticBNF::lex *		StaticBNF::buildGIMMEH(void)
{
  lex		*l19 = new lex;
  l19->key = DECL;
  l19->exp = VAR;
  l19->lol = "GIMMEH ";
  l19->cpp = "std::cin >> ";
  l19->lol_end = "";
  l19->cpp_end = ";";
  l19->do_convert = false;
  l19->priority = P_RIGHT;
  return l19;
}

void		StaticBNF::manageOperators(std::string & str)
{
  std::map<std::string, std::string>	op;

  op.insert(std::pair<std::string, std::string > (" UP ",  " + "));
  op.insert(std::pair<std::string, std::string > (" NERF ",  " - "));
  op.insert(std::pair<std::string, std::string > (" TIMEZ ",  " * "));
  op.insert(std::pair<std::string, std::string > (" OVAR ",  " / "));

  for (std::map<std::string, std::string>::iterator it = op.begin();
       it != op.end(); ++it)
    {
      size_t pos = str.find((*it).first);
      if (pos != std::string::npos)
	str.replace(pos, (*it).first.length(), (*it).second);
    }
}
